package com.zh.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.seckill.exception.GlobalException;
import com.zh.seckill.mapper.UserMapper;
import com.zh.seckill.pojo.User;
import com.zh.seckill.service.UserService;
import com.zh.seckill.util.CookieUtil;
import com.zh.seckill.util.MD5Util;
import com.zh.seckill.util.UUIDUtil;
import com.zh.seckill.vo.LoginVo;
import com.zh.seckill.vo.RespBean;
import com.zh.seckill.vo.RespBeanEnum;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(@Valid LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {

        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

//        // 检查用户登录信息的有效性
//        if (!StringUtils.hasText(mobile) || !StringUtils.hasText(password)) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
//        }
//        // 验证手机号格式是否正确
//        if (!ValidatorUtil.isMobile(mobile)) {
//            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
//        }

        // 根据手机号查询用户信息
        User user = userMapper.selectById(mobile);
        // 检查用户是否存在
        if (null == user) {
            //return RespBean.error(RespBeanEnum.MOBILE_NOT_EXIST);
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        // 验证密码是否正确
        if (!MD5Util.midPassToDBPass(password, user.getSlat()).equals(user.getPassword())) {
            // return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        // 登录成功
        String ticket = UUIDUtil.uuid();
        //System.out.println("redisTemplate-----" + redisTemplate.hashCode());
        request.getSession().setAttribute(ticket, user);

        redisTemplate.opsForValue().set("user:" + ticket, user);

        CookieUtil.setCookie(request, response, "userTicket", ticket);


        return RespBean.success(ticket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (!StringUtils.hasText(userTicket)) {
            return null;
        }

        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);

        if (user != null) {
            CookieUtil.setCookie(request, response, "userTicket", userTicket);
        }
        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {
        User user =
                getUserByCookie(userTicket, request, response);
        if (user == null) {
            //抛出异常
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }
        //设置新密码
        user.setPassword(MD5Util.inputPassToDBPass(password, user.getSlat()));
        int i = userMapper.updateById(user);
        if (i == 1) {//更新成功
            //删除该用户在Redis数据
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_FAIL);
    }
}

